/*
 * @Description:
 * @Author: Cxy
 * @Date: 2023-04-28 15:17:33
 * @LastEditors: Cxy
 * @LastEditTime: 2024-02-28 14:06:59
 * @FilePath: \blogAdmin\play\src\stores\power.ts
 */
import { defineStore } from 'pinia'
import { configStore } from './config'
import store from './index'
import type { RouteConfigInter } from '@/router/type'
import type { Ref } from 'vue'
import { dynamicProcessRO, dynamicRegExp, topLevelRO } from '@/router/utils'
import { staticDynamicRO } from '@/router/index'

const usePower = defineStore('power', () => {
  const allRD: Ref<RouteConfigInter[]> = ref([])
  const dynamicRD: Ref<DynamicInter[]> = ref([])
  const getPermissRouter = () => {
    return new Promise<void>(resolve => {
      setTimeout(() => {
        /* 菜单数据 */
        allRD.value = staticDynamicRO([
          {
            path: '/permission',
            meta: {
              title: 'menus.permission',
              icon: 'ep:medal'
            },
            children: [
              {
                path: 'permission',
                meta: {
                  title: 'menus.permission'
                },
                component: '/views/permission/index'
              }
            ]
          }
        ])
        setMenuData(configStore.config.menuMode)
        resolve()
      }, 500)
    })
  }

  /**
   * @description: 获取菜单模式数据
   */
  const headerRD: Ref<RouteConfigInter[]> = ref([])
  const navigaRD: Ref<RouteConfigInter[]> = ref([])
  const setMenuData = (mode: string) => {
    switch (mode) {
      case 'upMode':
        headerRD.value = allRD.value
        navigaRD.value = []
        break
      case 'leftMode':
        headerRD.value = []
        navigaRD.value = allRD.value
        break
      default: {
        const { pathname } = location
        const BASE_URL = import.meta.env.BASE_URL
        const path = dynamicProcessRO(pathname.replace(new RegExp(BASE_URL === '/' ? '' : BASE_URL), ''))
        headerRD.value = allRD.value
        const curR = allRD.value.find(
          c => c.path.replace(dynamicRegExp, '') === (topLevelRO(allRD.value, path)[0] || path)
        )
        if (curR?.children?.length) {
          navigaRD.value = curR.children
        } else if (curR) {
          navigaRD.value = [curR]
        }
        break
      }
    }
  }

  /**
   * @description: upLeftMode模式下的左侧菜单选择操作
   * @param {string} path 选中菜单path
   */
  const selectMenu = (path: string) => {
    const curR = allRD.value.find(c => c.path.replace(dynamicRegExp, '') === (topLevelRO(allRD.value, path)[0] || path))
    if (curR?.children?.length) {
      navigaRD.value = curR.children
    } else if (curR) {
      navigaRD.value = [curR]
    }
  }

  /**
   * @description: 判断菜单是否为分栏模式
   */
  const isColumnMode = computed(() => !!headerRD.value.length && !!navigaRD.value.length)

  return {
    allRD,
    dynamicRD,
    headerRD,
    navigaRD,
    isColumnMode,
    getPermissRouter,
    selectMenu,
    setMenuData
  }
})

export const powerStore = usePower(store)
